<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Tcl lexical structure</title>
<link rel="stylesheet" href="/cfg/format.css" type="text/css">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="Tcl, learn Tcl, lexical structure, programming, language, Linux">
<meta name="description" content="This chapter of the Tcl tutorial covers lexical structure of Tcl language.">
<meta name="language" content="en">
<meta name="author" content="Jan Bodnar">
<meta name="distribution" content="global">

<script type="text/javascript" src="/lib/jquery.js"></script>
<script type="text/javascript" src="/lib/common.js"></script>

</head>

<body>

<div class="container">

<div id="wide_ad" class="ltow">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* 160x600, August 2011 */
google_ad_slot = "2484182563";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>


<div class="content">

<a href="/" title="Home">Home</a>&nbsp;
<a href="..">Contents</a>



<h1>Tcl lexical structure</h1>

<p>
Computer languages, like human languages, have a lexical structure.
A lexis of a Tcl language consists of basic elements and rules
that apply to them. Words are basic elements in Tcl. Words can 
be either commands or command arguments. Substitution is one of the
basic rules of the Tcl grammar.
</p>

<script type="text/javascript"><!--
google_ad_client = "ca-pub-9706709751191532";
/* LargeSquare */
google_ad_slot = "5070696322";
google_ad_width = 336;
google_ad_height = 280;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

<h2>Commands</h2>

<p>
Tcl is a string-based, interpreted command language. 
A Tcl script consists of commands, which are separated by newlines
or semicolons. Commands are the basic execution elements. 
A command is followed by one or more words which are its arguments.
Each argument is separated by white space. 
</p>

<p>
A Tcl command has the following form : <code>command arg1 arg2 arg3 ...</code>
The Tcl interpreter takes each word of the sentence and evaluates it.
The first word is considered to be the command. Most Tcl commands are variadic. This means
that they can process variable number of arguments. 
</p>

<p>
When a Tcl script is parsed the commands are evaluated. Each command
interprets words in its own context. 
</p>

<pre class="code">
puts "Tcl language"
</pre>

<p>
In the above code excerpt, we have a <code>puts</code> command. This command
prints a message to the console. The <code>"Tcl language"</code> is a string, which
is being printed. Unlike in other languages, strings must not be enclosed in double
quotes. Unless there is a white space.
</p>

<pre class="code">
#!/usr/bin/tclsh

puts zetcode.com; puts androida.co

puts zetcode
puts androida
</pre>

<p>
In the first case the commands are separated by the semicolon (;) character. In the
second case they are separated by the newline character. 
</p>


<h2>Substitution</h2>

<p>
There are three kinds of substitutions in Tcl.
</p>

<ul>
  <li>Command substitution</li>
  <li>Variable substitution</li>
  <li>Backslash substitution</li>
</ul>

<p>
Square brackets are used for command substitution. 
</p>

<pre class="code">
% puts [expr 1+2]
3
</pre>

<p>
The <code>expr</code> command is used to perform arithmetic calculations. First
the command between the square brackets is evaluated and the result is
returned to the <code>puts</code> command. The <code>puts</code> command then 
evaluates the result and prints it to the console. 
</p>

<hr class="btm">

<p>
If a word contains a dollar sign ($), then Tcl performs variable substitution.
The dollar-sign and the following characters are replaced in the word by the 
value of a variable.
</p>

<pre class="code">
#!/usr/bin/tclsh

set name Jane

puts name
puts $name
</pre>

<p>
We create a variable called name and set a value to it. 
</p>

<pre class="explanation">
puts name
</pre>

<p>
In this case, we print a string "name" to the console.
</p>

<pre class="explanation">
puts $name
</pre>

<p>
In the second case, the argument is preceded by a $ character.
The value of the name variable is printed to the console. 
</p>

<pre class="code">
$ ./name.tcl 
name
Jane
</pre>

<p>
Output of the example.
</p>

<hr class="btm">

<p>
With the backslash substitution, we escape the original meaning of the character. 
For example, the \n stands for a new line. The \t is the tab character. 
</p>

<pre class="code">
% puts "This is \t Sparta"
This is          Sparta
</pre>

<p>
Here the \t sequence is replaced with a tab. 
</p>

<pre class="code">
#!/usr/bin/tclsh

puts "This was a \"great\" experience"
puts "The \\ character is the backslash character"

puts "20000\b\b miles"
</pre>

<p>
We use the backslash substitution, if we want to have
quotes inside quote characters. Also if we want to print
the \ character, we must precede it with additional backslash.
The \b is substituted with a backspace.
</p>

<pre class="code">
$ ./backslash.tcl 
This was a "great" experience
The \ character is the backslash character
200 miles
</pre>

<p>
Running the example.
</p>


<h2>Comments</h2>

<p>
Comments are used by humans to clarify the source code. 
In Tcl comments start with the # character. 
</p>

<pre class="code">
# example of a puts command
puts "Tcl language"
</pre>

<p>
All characters after the # character are ignored by tclsh. 
</p>

<pre class="code">
puts "Tcl language" ; # example of a puts command
</pre>

<p>
Inline comments are possible only if we use a semicolon.
</p>


<h2>White space</h2>

<p>
White space is used to separate words in Tcl source. It is also used to 
improve readability of the source code.
</p>

<pre class="code">
set name Jane
</pre>

<p>
The <code>set</code> command takes two parameters, which are separated by
white space. 
</p>

<pre class="code">
set age        32
set name       Robert
set occupation programmer
</pre>

<p>
We might use more spaces if we want to improve the clarity of the source code.
</p>

<pre class="code">
set vals { 1 2 3 4 5 6 7 }
puts $vals
</pre>

<p>
White space is used to separate items in Tcl lists. In C based languages, we
would use the comma character. 
</p>


<h2>Variables</h2>

<p>
A <b>variable</b> is an identifier, which holds a value. In programming 
we say, that we assign/set a value to a variable. Technically speaking, 
a variable is a reference to a computer memory, where the value is stored. 
Variable names are case sensitive. This means, that Name, name or NAME refer
to three different variables. 
</p>

<p>
Variables in Tcl are created with the <code>set</code> command. To obtain the 
value of a variable, its name is preceded with a $ character. 
</p>

<pre class="code">
#!/usr/bin/tclsh

set name Jane
set Name Julia
set NAME Erika

puts $name
puts $Name
puts $NAME
</pre>

<p>
In the above script we set three variables. The variable names are the same,
they only differ in case. This practice is however not recommended.
</p>

<pre>
$ ./case.tcl 
Jane
Julia
Erika
</pre>

<p>
Output.
</p>


<h2>Braces</h2>

<p>
Braces {} have special meaning in Tcl. 
Substitution of words is disabled inside braces. 
</p>

<pre class="code">
#!/usr/bin/tclsh

set name {Julia Novak}
puts $name

puts "Her name is $name"
puts {Her name is $name}
</pre>

<p>
This is a small script showing a usage of the braces in Tcl.
</p>

<pre class="explanation">
set name {Julia Novak}
</pre>

<p>
Braces can be used instead of double quotes to set strings 
separated by a white space.
</p>

<pre class="explanation">
puts "Her name is $name"
</pre>

<p>
Here the variable is substituted.
</p>

<pre class="explanation">
puts {Her name is $name}
</pre>

<p>
When using braces, the variable is not substituted. Everything
is printed literally. 
</p>

<pre>
$ ./braces.tcl 
Julia Novak
Her name is Julia Novak
Her name is $name
</pre>

<p>
Output of the braces.tcl script. 
</p>

<hr class="btm">

<pre class="code">
#!/usr/bin/tclsh


set numbers { 1 2 3 4 5 6 7 }
puts $numbers

puts "Braces {} are reserved characters in Tcl"
puts {Braces {} are reserved characters in Tcl}
</pre>

<p>
Braces are used to create lists. A list is a basic data type
in Tcl.
</p>

<pre class="explanation">
set numbers { 1 2 3 4 5 6 7 }
</pre>

<p>
Here a list of numbers is created.
</p>

<pre class="explanation">
puts "Braces {} are reserved characters in Tcl"
puts {Braces {} are reserved characters in Tcl}
</pre>

<p>
Braces inside double quotes or inside other braces are
taken as regular characters without special meaning. 
</p>

<pre>
$ ./braces2.tcl 
 1 2 3 4 5 6 7 
Braces {} are reserved characters in Tcl
Braces {} are reserved characters in Tcl
</pre>

<p>
Output of the braces2.tcl script. 
</p>


<h2>Square brackets</h2>

<p>
Square brackets, [], are used to create nested commands. These nested commands
are executed before the main command on the Tcl source line. They are used to
pass the result of one command as an argument to another command.
</p>

<pre class="code">
#!/usr/bin/tclsh

set cwd [pwd]
puts $cwd

puts [clock format [clock seconds] -format "%Y-%m-%d %T"]
</pre>

<p>
In the above code example, we show some nested commands. 
</p>

<pre class="explanation">
set cwd [pwd]
</pre>

<p>
The <code>pwd</code> command returns the current working directory
of the script. It is put between the square brackets, which makes
it a nested command. First the <code>pwd</code> command is executed
and then the result of the command is set to the cwd variable.
</p>

<pre class="explanation">
puts [clock format [clock seconds] -format "%Y-%m-%d %T"]
</pre>

<p>
Nested commands can be nested inside other nested commands. First the 
<code>clock seconds</code> is executed. It returns the current local time
in seconds. The result is passed to the <code>clock format</code> command, which
formats the time in a readable form. Finally, the formatted time is returned to
the <code>puts</code> command, which prints it to the console. 
</p>

<pre>
$ ./nested.tcl 
/home/vronskij/programming/tcl/lexis
2011-04-05 12:19:21
</pre>

<p>
Output of the example.
</p>


<h2>Quotes</h2>

<p>
Double quotes group words as a single argument to commands. Dollar signs,
square brackets and backslash are interpreted inside quotes.
</p>

<pre class="code">
#!/usr/bin/tclsh

set distro Ubuntu
puts "The Linux distribution name is $distro"

puts "The current working directory: [pwd]"
puts "2000000\b\b\b miles"
</pre>

<p>
This is a practical example of using quotes in Tcl.
</p>

<pre class="explanation">
puts "The Linux distribution name is $distro"
</pre>

<p>
The variable distro is evaluated inside the quote characters.
The $distro is replaced with "Ubuntu". 
</p>

<pre class="explanation">
puts "The current working directory: [pwd]"
</pre>

<p>
Commands inside square brackets are interpreted too. Here
we get the current working directory with the <code>pwd</code> command.
</p>

<pre class="explanation">
puts "2000000\b\b\b miles"
</pre>

<p>
The \b escape sequence deletes a preceding character. In our case three
zeros are deleted. 
</p>

<pre>
$ ./quotes.tcl 
The Linux distribution name is Ubuntu
The current working directory: /home/vronskij/programming/tcl/lexis
2000 miles
</pre>

<p>
Output of the quotes.tcl example.
</p>

<h2>Backslash</h2>

<p>
The backslash character can be used in three different ways in Tcl. 
It introduces some special characters, called escape sequences. These
can be newlines, tabs, backspaces among others. It escapes the meaning
of special Tcl characters ($, {}, "", \, ()). Finally, it can serve as
a line continuation character.
</p>

<pre class="code">
#!/usr/bin/tclsh

puts "0\t1"

set name Jane
puts \$name
puts \\$name

puts "He said: \"There are plenty of them\""
puts "There are currently many Linux\
distributions in the world"
</pre>

<p>
The above script shows how the backslash character can be used
in Tcl.
</p>

<pre class="explanation">
puts "0\t1"
</pre>

<p>
The \t character has a special meaning in Tcl. It stands for
a tab white space character. When we execute the script, 8
spaces are put inside 0 and 1. 
</p>

<pre class="explanation">
puts \$name
</pre>

<p>
With the backslash, we escape the meaning of the dollar sign. 
In our case $name characters are printed to the console.
</p>

<pre class="explanation">
puts \\$name
</pre>

<p>
The backslash can be escaped too. Here the backslash character
and the value of the name variable are printed to the
console. 
</p>

<pre class="explanation">
puts "He said: \"There are plenty of them\""
</pre>

<p>
We can form direct speech by escaping the meaning of the inner
double quotes. 
</p>

<pre class="explanation">
puts "There are currently many Linux\
distributions in the world"
</pre>

<p>
If the source line is too wide, we can continue on the next
line using the backslash character. Effectively escaping
the newline.
</p>

<pre>
$ ./backslash.tcl 
0       1
$name
\Jane
He said: "There are plenty of them"
There are currently many Linux distributions in the world
</pre>

<p>
Running the example.
</p>

<h2>Round brackets</h2>

<p>
Round brackets are used to indicate an array subscript or to 
change the precedence of operators for the <code>expr</code>
command.
</p>

<pre class="code">
#!/usr/bin/tclsh

set names(1) Jane
set names(2) Robert

puts $names(1)
puts $names(2)

puts [expr (1+3)*5]
</pre>

<p>
This is a simple example with round brackets in Tcl.
</p>

<pre class="explanation">
puts $names(1)
</pre>

<p>
We use the round brackets to access the value by a key
which is specified inside round brackets. 
</p>

<pre class="explanation">
puts [expr (1+3)*5]
</pre>

<p>
Here we change the precedence for operators. First 1 and 3 are added and
then the result is multiplied by 5. 
</p>

<p>
In this chapter we have described the lexis of the Tcl language. 
</p>

<br>
<div class="center"> 
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* horizontal */
google_ad_slot = "1734478269";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script> 
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script> 
</div> 
<br>


<div class="botNav, center">
<span class="botNavItem"><a href="/">Home</a></span> ‡ <span class="botNavItem"><a href="..">Contents</a></span> ‡
<span class="botNavItem"><a href="#">Top of Page</a></span>
</div>


<div class="footer">
<div class="signature">
<a href="/">ZetCode</a> last modified April 5, 2011  <span class="copyright">&copy; 2007 - 2013 Jan Bodnar</span>
</div>
</div>

</div> <!-- content -->

</div> <!-- container -->

</body>
</html>
